package com.cykj.order.config;

/**
 * @BelongsProject: Cykj-Order-System
 * @BelongsPackage: com.cykj.order.config
 * @Author: cwx
 * @CreateTime: 2025/9/5 12:44
 * @Description:
 * @Version: 1.0
 */


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlipayClientConfig {

    @Autowired
    private AliPayConfig aliPayConfig; // 注入已配置的支付宝参数

    @Bean(name = "orderAlipayClient")
    public AlipayClient createAlipayClient() {
        try {
            return new DefaultAlipayClient(
                    aliPayConfig.getGatewayUrl(),    // 沙箱网关（配置中已正确设置）
                    aliPayConfig.getAppId(),         // 沙箱APPID
                    aliPayConfig.getAppPrivateKey(), // 应用私钥（配置中需确保正确）
                    "json",                          // 格式固定为json
                    aliPayConfig.getCharset(),       // 编码（UTF-8）
                    aliPayConfig.getAlipayPublicKey(),// 支付宝公钥（需与沙箱匹配）
                    aliPayConfig.getSignType()       // 签名方式（RSA2）
            );
        } catch (Exception e) {
            throw new RuntimeException("AlipayClient初始化失败：" + e.getMessage());
        }
    }
}